<div class="layuimini-container layuimini-page-anim" divname="deviceRemote">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备SN</label>
                            <div class="layui-input-inline">
                                <input type="text" maxlength="16" name="DeviceID" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">设备名称</label>
                            <div class="layui-input-inline">
                                <input type="text" maxlength="30" name="DeviceName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">操作类型</label>
                            <div class="layui-input-inline">
                                <select name="remote" lay-verify="">
                                    <option value="null" selected>(无)</option>
                                    <option value="opendoor">远程开门</option>
                                    <option value="restart">远程重启</option>
                                    <option value="closealarm">解除报警</option>
                                    <option value="reset">恢复出厂</option>
                                    <option value="emptyPeople">清空所有人员</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <!-- <button id="btntest" type="submit" class="layui-btn layui-btn-primary" lay-filter="data-test-btn">
                                <i class="layui-icon"></i> 测试
                            </button>
                            -->
                        </div>
                    </div>
                </form>
            </div>

        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">

                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="reload"> 刷新 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-delete-btn" lay-event="clearRemote"> 清空所有操作 </button>
            </div>
        </script>

        <table class="layui-hide" id="DeviceRemotePageTable" lay-filter="DeviceRemotePageTableFilter"></table>

    </div>
</div>
<style>
    .label_run {
        color: #0034FF;
    }

    .label_wait {
        color: #8C7F12;
    }
</style>

<script>

    layui.use(['form', 'table', 'miniPage', 'element', 'faceAPI', 'dropdown'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var faceAPI = layui.faceAPI;
        var tabDatas = null;
        var dropdown = layui.dropdown;

        form.render();




        //$('#btntest').on('click', function () {
        //    var that = this;
        //    layer.tips('只想提示地精准些', that); //在元素的事件回调体中，follow直接赋予this即可
        //});

        var RemoteTypeTable = [];
        RemoteTypeTable[1] = "远程开门";//1，远程开门；2、远程重启；3、恢复出厂设置；4、关闭报警
        RemoteTypeTable[2] = "远程重启";
        RemoteTypeTable[3] = "恢复出厂设置";
        RemoteTypeTable[4] = "关闭报警";
        RemoteTypeTable[5] = "清空所有人员";

        var RemoteStatusTable = [];
        RemoteStatusTable[0] = "未执行";
        RemoteStatusTable[1] = "已执行";

        table.render({
            elem: '#DeviceRemotePageTable',
            //**************************异步请求开始***********************
            url: 'api/DeviceRemote/GetList',
            method: 'post',
            where: { remote: 'null' },
            headers: {
                //token: 'sasasas',
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else {
                    /*layer.open({
                        title: '请求错误',
                        content: '(' + res.errCode + ')' + res.error
                    });  */

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data": [] //解析数据列表
                    };
                }

            },
            //**************************异步请求结束***********************

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'createTime', width: 180, title: '创建时间', sort: true },
                { field: 'deviceSN', width: 170, title: 'SN', sort: true },
                { field: 'deviceName', width: 200, title: '名称', sort: true },
                {
                    field: 'taskType', width: 180, title: '操作',
                    templet: function (d) {
                        return RemoteTypeTable[d.taskType];
                    }
                },
                {//上传状态：0--未执行；1--已执行
                    field: 'taskStatus', width: 120, title: '状态',
                    templet: function (d) {
                        var stip;
                        stip = '<span class="label ';
                        if (d.taskStatus == 0)
                            stip = stip + '  label_wait">';
                        else
                            stip = stip + '  label_run">';
                        stip = stip + RemoteStatusTable[d.taskStatus] + '</span>';
                        return stip;
                    }
                },
                {
                    field: 'taskRunTime', width: 180, title: '执行时间',
                    templet: function (d) {
                        if (d.taskStatus == 1) {
                            return d.taskRunTime;
                        }
                        else
                            return '-';

                    }
                }
            ]],
            limits: [100, 200, 300, 400, 500, 600],
            limit: 100,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = data.field;
            //执行搜索重载
            table.reload('DeviceRemotePageTable', {
                page: {
                    curr: 1
                }
                , where: result
            }, 'data');

            return false;
        });


        /**
         * toolbar事件监听
         */
        table.on('toolbar(DeviceRemotePageTableFilter)', function (obj) {
            if (obj.event === 'clearRemote') {   // 监听添加操作
                layer.confirm('操作确认：清空所有远程操作记录？', function (index) {
                    faceAPI.Ajax({
                        type: 'Delete',
                        url: '/api/DeviceRemote/ClearRemote',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                table.reload('DeviceRemotePageTable', {
                                    page: {
                                        curr: 1
                                    }
                                }, 'data');
                            }

                        }
                    });
                    layer.close(index);
                });

            }
            else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('DeviceRemotePageTable')
                    , data = checkStatus.data;
                var TaskIDs = [];
                //console.log(data);
                data.forEach(function (item, index) { TaskIDs.push(item.taskID); });
                if (TaskIDs.length == 0) {

                    layer.alert('请勾选需要删除的远程操作');
                    return;
                }
                debugger;
                faceAPI.Ajax({
                    type: 'Post',
                    data: { TaskIDs },
                    url: '/api/DeviceRemote/Delete',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            table.reload('DeviceRemotePageTable', {
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                        }

                    }
                });
            }
            else if (obj.event === 'reload') {
                table.reload('DeviceRemotePageTable', {
                    page: {
                        curr: 1
                    }
                }, 'data');
            }
        });

    });



</script>


